package firstping.string;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {
	
	public static String md5(String str) throws NoSuchAlgorithmException {
		MessageDigest md;
		str = str+"0089176";
		md = MessageDigest.getInstance("MD5");
		md.update(str.getBytes());

		byte byteData[] = md.digest();

		// convert the byte to hex format method 1
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < byteData.length; i++) {
			sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16)
					.substring(1));
		}

		str = sb.toString();
		return str;
	}
	public static String fomatDomain(String str)
	{
		if (str != null)
			str = str.toLowerCase().replaceAll("(http|https)+://", "")
					.replaceAll("^[\\w\\W]+[\\/]{2}", "")
					.replaceAll("[\\/]+[\\w\\W]*$", "")
					.replaceAll("[\\?]+[\\w\\W]*$", "")
					.replaceAll("[ !@#$%^&*?()\\[\\]{}+=,:;|\\\'\\\"/\\\\`~]+", "")
					.trim();

		return str;
	}
	public static Boolean checkDomain(String domain)
	{
		ResourceBundle resource = ResourceBundle.getBundle("domain", new Locale("en"));
		String re_domain = domain;

    	String[] listDomain = resource.getString("domain").split(" ");
    	for(int i=0;i<listDomain.length;i++)
    	{
    		re_domain = re_domain.replaceAll("[.]{1}"+listDomain[i]+"$", "");
    	}

        
        if(re_domain.length() != domain.length())
        {
        	return true;
        }
		
		return false;
	}
	
	public static String textCrop(String content,String begin, String end)
	{
		 int start = content.indexOf(begin)+begin.length();
		    
		 int finish = content.indexOf(end,start); 
		 return content.substring(start, finish);
		    
		
	}
	public static String convertToString(List list)
	{
		
		String str="";
		for(Object jString : list)
		{
			str+=jString.toString()+",";
			
		}
		return str;
	}
	public static String convert2DomainString(List list)
	{
		
		String str="";
		int k=0;
		for(Object jString : list)
		{
			
			str+="<a href=\"/sites/"+jString+"\">"+jString+"</a> ";
			k++;
			if(k >= 14)break;
			
		}
		return str;
	}
	
	public static void main( String args[])
	{
		
		System.out.println(textCrop("I-php.net has a three-month global Alexa traffic rank of 1,583,359. Visitors to the site view 4.7 unique pages each day on average. Approximately 44% of visits to it are bounces (one pageview only). Visitors to the site spend about 49 seconds on each pageview and a total of seven minutes on the site during each visit. Almost all visitors to I-php.net come from Vietnam, where it has attained a traffic rank of 7,141.","has a three-month global Alexa traffic rank of","Visitors to the site view"));
	}
	
}
